<?php
class admin extends spController
{
	function top() {
		$this->display('admin/top.html');
	}

	function menu() {
		$this->display('admin/menu.html');
	}

	function main() {
		$this->display('admin/main.html');
	}

	function index() {
		$this->display('admin/index.html');
	}

	function userList() {
		$conditionss = array();
		$sort		 = '';
		$fields      = '';
		$limit		 = '';

		$mUser = spClass('user');
		$users = $mUser->findAll($conditions, $sort, $fields, $limit);
		$this->users = $users;
		$this->display('admin/userList.html');
	}

	function showUser() {
		$uid = $this->spArgs('uid', '1807722553');

		$mUser = spClass('user');
		$user  = $mUser->find(array('uid'=>$uid));
		dump($user);
	}

	function editUser() {
		$uid = $this->spArgs('uid', '1807722553');

		$mUser = spClass('user');
		$conditions = array('uid'=>$uid);
		$row   = array('cash' => '3800');
		$msg   = $mUser->update($conditions, $row);
		if ($msg) {
			$this->success('Edit Success', spUrl('admin', 'showuser', array('uid'=>$uid)));
		} else {
			$this->error('Edit failed', spUrl('admin', 'showuser', array('uid'=>$uid)));
		}
	}

	function blogList() {
		$conditionss = array();
		$sort		 = '';
		$fields		 = 'id, title, dateline, read';
		$limit		 = '';

		$mBlog = spClass('blog');
		$blogs = $mblog->findAll($conditions, $sort, $fields, $limit);
		dump($blogs);
	}

	function addBlog() {
		$blogData= $this->spArgs('blog');
		if (is_array($blogData)) {
			$blogData['dateline'] = time();
			$mBlog = spClass('blog');
			$newId = $mBlog->create($blogData);
			if ($newId) {
				$this->success('Create blog Success', spUrl('admin', 'showBlog', array('id'=>$newId)));
			} else {
				$this->error('Create failed', spUrl('admin', 'addBlog', $blogData));
			}
		} else {
			echo 'add blog html';
		}
	}

	function editBlog() {
		$blogId  = $this->spArgs('id');
		$blogData= $this->spArgs('blog');
		$mBlog = spClass('blog');
		$ret   = $mBlog->update(array('id'=>$blogId), $blogData);
		if ($ret) {
			$this->success('Edit blog Success', spUrl('admin', 'showBlog', array('id'=>$blogId)));
		} else {
			$this->error('Edit failed', spUrl('admin', 'blogList'));
		}
	}

	function deleteBlog() {
		$blogId  = $this->spArgs('id');
		$mBlog = spClass('blog');
		if ($mBlog->update(array('id'=>$blogId), array('active'=>false))) {
			$this->success('Delete blog Success', spUrl('admin', 'blogList'));
		} else {
			$this->error('Delete failed', spUrl('admin', 'blogList'));
		}
	}

	function noticeList() {
		$conditionss = array();
		$sort		 = '';
		$fields		 = '';
		$limit		 = '';

		$mNotice = spClass('notice');
		$notices = $mblog->findAll($conditions, $sort, $fields, $limit);
		dump($blogs);
	}

	function addNotice() {
		$noticeData= $this->spArgs('notice');
		if (is_array($noticeData)) {
			$noticeData['dateline'] = time();
			$mNotice = spClass('notice');
			$newId = $mBlog->create($noticeData);
			if ($newId) {
				$this->success('Create notice Success', spUrl('admin', 'noticeList'));
			} else {
				$this->error('Create failed', spUrl('admin', 'noticeList'));
			}
		} else {
			echo 'add notice html';
		}
	}

	function editNotice() {
		$noticeId  = $this->spArgs('id');
		$noticeData= $this->spArgs('notice');
		$mNotice = spClass('notice');
		$ret   = $mNotice->update(array('id'=>$noticeId), $noticeData);
		if ($ret) {
			$this->success('Edit notice Success', spUrl('admin', 'noticeList'));
		} else {
			$this->error('Edit failed', spUrl('admin', 'noticeList'));
		}
	}

	function deleteNotice() {
		$noticeId  = $this->spArgs('id');
		$mNotice = spClass('notice');
		if ($mNotice->update(array('id'=>$noticeId), array('active'=>false))) {
			$this->success('Delete notice Success', spUrl('admin', 'noticeList'));
		} else {
			$this->error('Delete failed', spUrl('admin', 'noticeList'));
		}
	}

	function ticketList() {
		$conditionss = array();
		$sort		 = '';
		$fields		 = '';
		$limit		 = '';

		$mTicket = spClass('ticket');
		$tickets = $mTicket->findAll($conditions, $sort, $fields, $limit);
		$this->tickets = $tickets;
		$this->display('admin/ticketList.html');
	}

	function addTicket() {
		$ticketData= $this->spArgs('ticket');
		$mTicket = spClass('ticket');

		if (is_array($ticketData)) {
			if( false == $mTicket->spVerifier($ticketData)){
				$ticketData['dateline'] = time();
				$newId = $mTicket->create($ticketData);
				if ($newId) {
					$this->success('Create ticket Success', spUrl('admin', 'ticketList'));
				} else {
					$this->error('Create failed', spUrl('admin', 'addTicket', $ticketData));
				}
			} else {
				dump($mTicket->spVerifier($ticketData));
			}
		} else {
			$this->display('admin/addTicket.html');
		}
	}

	function addCard() {
		$ticketId = $this->spArgs('tid');
		$cardData = $this->spArgs('card');
		if (is_array($cardData)) {
			$mCard = spClass('card');
			foreach ($cardData['number'] as $key=>$number) {
				if ($number) {
					$row['number']   = $number;
					$row['password'] = $cardData['password'][$key];
					$row['tid']		 = $ticketId;
					$row['uid']		 = $_SESSION['uid'];
					$row['active']   = true;
					$mCard->create($row);
				}
			}
			$this->success('Create card Success', spUrl('admin', 'cardList', array('tid'=>$ticketId)));
		} else {
			$mTicket = spClass('ticket');
			$ticket  = $mTicket->find(array('id'=>$ticketId));
			$this->ticketName = $ticket['name'];
			$this->tid		  = $ticketId;
			$this->display('admin/addCard.html');
		}
	}

	function cardList() {
		$ticketId = $this->spArgs('tid');
		$mTicket = spClass('ticket');
		$ticket  = $mTicket->find(array('id'=>$ticketId));
		$this->ticket = $ticket;

		$mCard = spClass('card');
		$cards = $mCard->findAll(array('tid'=>$ticketId));
		$this->cards = $cards;
		$this->display('admin/cardList.html');
	}

	function addGift() {
		$giftData= $this->spArgs('gift');

		$mGiftCat = spClass('giftcat');
		$results  = $mGiftCat->findAll();
		foreach ($results as $row) {
			$giftCats[$row['id']] = $row['title'];
		}
		$this->giftcats = $giftCats;

		$mGift = spClass('gift');
		if (is_array($giftData)) {
			$giftData['dateline'] = time();
			$newId = $mGift->create($giftData);
			if ($newId) {
				$this->success('Create gift Success', spUrl('admin', 'giftList'));
			} else {
				$this->error('Create failed', spUrl('admin', 'addgift', $giftData));
			}
		} else {
			$this->display('admin/addGift.html');
		}
	}

	function giftList() {
		$conditionss = array();
		$sort		 = '';
		$fields		 = '';
		$limit		 = '';

		$mGift = spClass('gift');
		$gifts = $mGift->findAll($conditions, $sort, $fields, $limit);
		$this->gifts = $gifts;
		$this->display('admin/giftList.html');
	}
}